package com.sparrow.manage.template.data.strategy;

import com.sparrow.manage.article.Article;
import com.sparrow.manage.article.ArticleService;
import com.sparrow.manage.common.entity.Attachment;
import com.sparrow.manage.common.service.impl.AttachmentService;
import com.sparrow.manage.company.Company;
import com.sparrow.manage.company.CompanyService;
import com.sparrow.manage.tab.Tab;

import java.util.*;

public class CommonDataFetch implements IDataFetch{

    private ArticleService articleService;

    private CompanyService companyService;

    private AttachmentService attachmentService;

    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }

    /**
     * 得到的变量有: <br/>
     *  company : com.sparrow.manage.company.Company  // 公司信息 <br/>
     *  introduce : com.sparrow.manage.article.Article    // 公司简介 <br/>
     *  carousels : List&lt;Article&gt;   // 轮播图集合, 最多三张 <br/>
     *  events : List&lt;Article&gt;    // 要闻集合, 最多三个 <br/>
     * @return
     */
    @Override
    public Map<String, Object> getVariables() {
        if(companyService == null){
            throw new IllegalStateException("companyService is null.");
        }
        if(articleService == null){
            throw new IllegalStateException("articleService is null.");
        }

        Map<String, Object> map = new HashMap<>();
        Set<Article> allArticles = new HashSet<>();
        map.put("all", allArticles);

        Company company = companyService.queryOne();
        map.put("company", company);

        List<Article> introduces = articleService.queryByType(Article.ArticleType.COMPANY, 1);
        if(!introduces.isEmpty()){
            allArticles.add(introduces.get(0));
            map.put("introduce", introduces.get(0));
        }else{
            map.put("introduce", new Article());
        }

        List<Article> events = articleService.queryByType(Article.ArticleType.EVENT, 3);
        this.pushHeadImages(events);
        allArticles.addAll(events);
        map.put("events", events);

        List<Article> carousels = articleService.queryCarousel(3);
        this.pushHeadImages(carousels);
        allArticles.addAll(carousels);
        map.put("carousels", carousels);

        List<String> bigImgs = new ArrayList<>(3);
        map.put("bigImgs", bigImgs);

        for(int i = 0, len = carousels.size(); i < len; i++){
            bigImgs.add(carousels.get(i).getHeadImage().getLocation());
        }
        if(bigImgs.isEmpty()){
            bigImgs.add("/banner.jpg");
        }
        if(bigImgs.size() < 3){
            for(int i = 0; i < 3; i++){
                bigImgs.add(bigImgs.get(i));
            }
        }


        List<Article> members = articleService.queryByType(Article.ArticleType.EMPLOYEE, 4);
        this.pushHeadImages(members);
        allArticles.addAll(members);
        map.put("members", members);

        List<Article> services = articleService.queryByType(Article.ArticleType.BUSINESS, 6);
        this.pushHeadImages(services);
        allArticles.addAll(services);
        map.put("services", services);

        map.put("tabs", getTabs());

        return map;
    }

    private List<Tab> getTabs(){
        List<Tab> tabs = new ArrayList<>();

        Tab tab1 = new Tab();
        tab1.setName("首页");
        tab1.setEname("index");
        tab1.setUrl("index");
        tabs.add(tab1);

        Tab tab2 = new Tab();
        tab2.setName("关于");
        tab2.setEname("about");
        tab2.setUrl("about");
        tabs.add(tab2);


        Tab tab3 = new Tab();
        tab3.setName("印象");
        tab3.setEname("gallery");
        tab3.setUrl("gallery");
        tabs.add(tab3);

        Tab tab5 = new Tab();
        tab5.setName("联系我们");
        tab5.setEname("contact");
        tab5.setUrl("contact");
        tabs.add(tab5);

        return tabs;
    }

    private void pushHeadImages(List<Article> articles){
        if(!articles.isEmpty()){
            for (Article article : articles){
                List<Attachment> atts = attachmentService.queryByRelateId(article.getId());
                if(atts != null && !atts.isEmpty()){
                    article.setHeadImage(atts.get(0));
                }
            }
        }
    }
}
